package com.iceland.common.listener;

public abstract class ConsumeListener<T> {
    long timeout=-1;//超时时间
    long expireTime=Long.MAX_VALUE;//过期时间：由超时时间已经调用updateExpireTime的时间决定
    public ConsumeListener(){}
    public ConsumeListener(long timeout){
        this.timeout=timeout;
    }

    protected abstract void consume(T element);

    protected abstract void expire(T element);

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public long getExpireTime() {
        return expireTime;
    }

    public void updateExpireTime() {
        if(timeout>0){
            this.expireTime = System.currentTimeMillis()+timeout;
        }
    }

    /**
     * 监听器是否过期
     * @return
     */
    public boolean isExpire(){
        return System.currentTimeMillis()>expireTime;
    }
}
